package com.example.main;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.sax.StartElementListener;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mattrigle.ControlApp;
import com.example.mattrigle.GameCanvas;
import com.example.mattrigle.MainActivity;
import com.example.mattrigle.R;
import com.example.mattrigle.SaveData;
import com.example.mattrigle.Sound.SoundGame;

public class Game_Dialog{

	public int r = 0;
	public int g = 255;	
	public float i;
	public float j;
	public float k;
	private GameCanvas _gameView_correct;
	private GameCanvas _gameView_answer;
//	private SoundGame soundGame;
	private Context contexts;
	@SuppressLint("NewApi")
	 public void Dialog(Activity activity,final Context context,boolean isCos, final float angle_correct, final float angle_answer, boolean isGameOver, final int oleScore, int sureness){
		
//		soundGame = new SoundGame();
		this.contexts = context;
		final Dialog dialog = new Dialog(activity,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_game);
		
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		
		LinearLayout lay_correct = (LinearLayout) dialog.findViewById(R.id.lay_correct);
		LinearLayout lay_answer = (LinearLayout) dialog.findViewById(R.id.lay_answer);
		FrameLayout btn_next = (FrameLayout) dialog.findViewById(R.id.BtnNext);		
		FrameLayout btn_ok = (FrameLayout) dialog.findViewById(R.id.BtnOK);
		TextView txt_correct_radius = (TextView) dialog.findViewById(R.id.txt_correct_radius);
		final TextView txt_answer_radius = (TextView) dialog.findViewById(R.id.txt_answer_radius);
		final TextView txt_score = (TextView) dialog.findViewById(R.id.txt_score);
		TextView txt_header_dialog = (TextView) dialog.findViewById(R.id.txt_head_dialog);
		final LinearLayout circle_color = (LinearLayout) dialog.findViewById(R.id.circle_color);
		
		// +-- Set Color --+ //
		/*
		float radius = Math.abs(angle_correct - angle_answer);
		r = 0;
		g = 255;
		int diff = (int)((500 * radius)/90)+100;
		//Toast.makeText(activity, ""+diff, Toast.LENGTH_LONG).show();
		r = r + diff;
		if(r >= 255){
			int a = r - 255;
			r = 255;
			g = g - a;
		}
				
		GradientDrawable bg = new GradientDrawable();
		bg.setColor(Color.rgb(r, g, 0));
		bg.setShape(GradientDrawable.OVAL);
		
		circle_color.setBackground(bg);
		*/
		final float radius = Math.abs(angle_correct - angle_answer);
		j = 0.0f;		
		final Handler handler1 = new Handler();
		handler1.post(new Runnable() {
			
			@Override
			public void run() {
				
				r = 0;
				g = 255;
				int diff = (int)((500 * j)/90)+100;
				//Toast.makeText(activity, ""+diff, Toast.LENGTH_LONG).show();
				r = r + diff;
				if(r >= 255){
					int a = r - 255;
					r = 255;
					g = g - a;
				}
				//-----comment por		
//				GradientDrawable bg = new GradientDrawable();
//				bg.setColor(Color.rgb(r, g, 0));
//				bg.setShape(GradientDrawable.OVAL);
//				
//				circle_color.setBackground(bg);
				
				j+=0.1f;
				if(j<=(radius)){
					handler1.postDelayed(this, 1);
				}				
			}
		});
		
		if(angle_correct > angle_answer){
			k = Math.round(angle_correct);
			final Handler handler2 = new Handler();
			handler2.post(new Runnable() {
				
				@Override
				public void run() {
					k = (float) (k-0.1);
					DecimalFormat df = new DecimalFormat("#.#");
					txt_answer_radius.setText(df.format(k));
					if(k > angle_answer+0.1f){
						handler2.postDelayed(this, 1);
					}
				}
			});
		}else if(angle_answer > angle_correct){
			k = Math.round(angle_correct);
			final Handler handler2 = new Handler();
			handler2.post(new Runnable() {
				
				@Override
				public void run() {
					k = (float) (k+0.1);
					DecimalFormat df = new DecimalFormat("#.#");
					txt_answer_radius.setText(df.format(k));
					if(k < angle_answer-0.1f){
						handler2.postDelayed(this, 1);
					}
				}
			});
		}else {
			txt_answer_radius.setText(Float.toString(angle_answer));
		}
		
		
		
		// +-- Set Score --+ //
		
		final float upscoure = Math.round(((sureness * 10)-radius));
		final float sum = (float) ((0.1*upscoure)/radius);
		//Toast.makeText(context, sum+"", Toast.LENGTH_LONG).show();
		i = oleScore;
		final Handler handler = new Handler();		
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				txt_score.setText("Score : "+Integer.toString(Math.round(i)));
				//Log.e("arm", i+"");
				i = i+(sum);
				if(sum < 0){
					if(i >= (upscoure+oleScore)+4){
						handler.postDelayed(this, 1);
					}else if(i >= upscoure+oleScore) {
						handler.postDelayed(this, 1);
					}else{
						//Log.d("arm", "yess");
						Animation card_in2 = AnimationUtils.loadAnimation(context, R.anim.card_in2);
						txt_score.startAnimation(card_in2);
					}
				}else{
					if(i<=(upscoure+oleScore)-4){
						handler.postDelayed(this, 1);
					}else if(i <= (upscoure+oleScore)){
						handler.postDelayed(this, 1);
					}else{
						//Log.i("arm", "yess");
						Animation card_in2 = AnimationUtils.loadAnimation(context, R.anim.card_in2);
						txt_score.startAnimation(card_in2);
					}
				}
				
			}
		});
		
		
		// +-- Set Text --+ //
		txt_correct_radius.setText(Float.toString(angle_correct));
		//txt_answer_radius.setText(Float.toString(angle_answer));
		//txt_score.setText("Score : "+Integer.toString(Score));
				
		// +-- Check GameOver --+ //
		if(isGameOver){
			txt_header_dialog.setText("Game Over");
			btn_next.setVisibility(View.GONE);
		}else{
			txt_header_dialog.setText("Complete");
			btn_ok.setVisibility(View.GONE);
		}
		
		// +-- Set onClick --+ //
		btn_next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundGame.getInstance().playSoundButton(contexts);
				dialog.dismiss();
				//InitNewChapter();
			}
		});
		
		btn_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundGame.getInstance().playSoundButton(contexts);
				dialog.dismiss();
				//InitNewChapter();
			}
		});
		
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
		{
		    @Override
		    public void onCancel(DialogInterface dialog)
		    {
		    	//InitNewChapter();
		    }
		});
		
		//========================//
		// -- GameView Correct -- //
		//========================//
				
		_gameView_correct = new GameCanvas(context, true);
		_gameView_correct.setLineWidth(10);
		_gameView_correct.newStageWithAngle(isCos, angle_correct);
		lay_correct.addView(_gameView_correct, new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
					
		_gameView_correct.LockSeekBar(true);
		//=======================//
		// -- GameView Answer -- //
		//=======================//
		_gameView_answer = new GameCanvas(context, true);
		_gameView_answer.setLineWidth(10);
		_gameView_answer.newStageWithAngle(isCos, angle_answer);
		lay_answer.addView(_gameView_answer, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
				
		_gameView_answer.LockSeekBar(true);
		
		dialog.show();		
	}
		
	
	
	
	Editable user_name;
	Editable user_lname;
	
	

	
	public void dialog_user(final Context activity,final boolean ExitToCloseApp){
		
		this.contexts = activity;
		final Dialog user = new Dialog(activity,android.R.style.Theme_Translucent_NoTitleBar);
		user.requestWindowFeature(user.getWindow().FEATURE_NO_TITLE);
		user.setContentView(R.layout.dialog_login);
		user.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		user.setCancelable(false); 

		
		LinearLayout btn_ok = (LinearLayout) user.findViewById(R.id.btnOK);
		FrameLayout btn_exit = (FrameLayout) user.findViewById(R.id.btnExit);
		final EditText edit_name = (EditText) user.findViewById(R.id.name);
		
		
		edit_name.setText(ControlApp.Username);
//		edit_lname.setText(ControlApp.SerUsername);
		
		user_name = edit_name.getText();
		edit_name.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/THA0085.ttf"));
		btn_ok.setOnClickListener(new OnClickListener() {				
				@Override
				public void onClick(View v) {
					SoundGame.getInstance().playSoundButton(activity);
					if(!user_name.toString().equals("")){
//						if(user_lname.toString().equals("")){
							SaveData.PlayerName =  user_name.toString();
							
							
							ControlApp.Username=user_name.toString();
//							ControlApp.SerUsername=user_lname.toString();
							
							user.dismiss();
						/*}else{
							Toast.makeText(activity, "เธ�เธฃเธธเธ“เธฒเธฃเธฐเธ�เธธเธ�เธฒเธกเธชเธ�เธธเธฅ", Toast.LENGTH_LONG).show();
						}*/
					}else{
						Toast.makeText(activity, "กรุณากรอกชื่อด้วยต่ะ", Toast.LENGTH_LONG).show();
					}	
				}
			});
		
		btn_exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundGame.getInstance().playSoundButton(activity);
				if(ExitToCloseApp)
				{
					
					((Activity)activity).finish();
					_gameView_answer.onFinish();
					_gameView_answer.onFinish();
				}
				else
					user.dismiss();
			}
		});
		
		
		user.show();
		
	}
	
	public void dialog_degree(final Context activity){
		final SoundGame soundGame;
		soundGame = new SoundGame();
		final Dialog degree = new Dialog(activity,android.R.style.Theme_Translucent_NoTitleBar);
		degree.requestWindowFeature(degree.getWindow().FEATURE_NO_TITLE);
		degree.setContentView(R.layout.dialog_degree);
		degree.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		
		ImageView degree30 = (ImageView) degree.findViewById(R.id.degree30);
		ImageView degree45 = (ImageView) degree.findViewById(R.id.degree45);
		ImageView degree60 = (ImageView) degree.findViewById(R.id.degree60);
		
		
		float scale = activity.getResources().getDisplayMetrics().density;    
		Log.e("test", "scale:"+scale);
		
		
		
		int h=(int)(scale*50);
		int w=(int)(scale*50);
		
		degree30.setImageResource(R.drawable.thirty_a);
		degree60.setImageResource(R.drawable.sixty_a);
		degree30.getLayoutParams().height = (int)(100*scale);
		degree45.getLayoutParams().height = (int)(150*scale);
		degree60.getLayoutParams().height = (int)(100*scale);
		
		
		
		TextView txtTitle = (TextView)degree.findViewById(R.id.txtTitleDeegree);
		txtTitle.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/THA0085.ttf"));
		
		degree30.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// degree30
				//activity.finish();
//				soundGame.playSoundButton(activity);
//				SaveData.Angle = "30";
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mms-s");
//				SaveData.DateTime_StartTask = sdf.format(new Date());
//				MainActivity.Chapter=0;
//				SoundGame.getInstance().stopSound();
//				activity.startActivity(new Intent(activity,MainActivity.class));
//				Activity activi = (Activity) activity;
//				activi.finish();
//				degree.dismiss();
				
			}
		});
		
		degree45.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// degree45
				soundGame.playSoundButton(activity);
				SaveData.Angle = "45";
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H-m-s");
				SaveData.DateTime_StartTask = sdf.format(new Date());
				
				MainActivity.Chapter=1;

				SoundGame.getInstance().stopSound();

				activity.startActivity(new Intent(activity,MainActivity.class));
				Activity activi = (Activity) activity;
				activi.finish();
				degree.dismiss();
			}
		});

		degree60.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// degree60
//				soundGame.playSoundButton(activity);
//				SaveData.Angle = "60";
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mms-s");
//				SaveData.DateTime_StartTask = sdf.format(new Date());
//				
//				MainActivity.Chapter=2;
//
//				SoundGame.getInstance().stopSound();
//				activity.startActivity(new Intent(activity,MainActivity.class));
//				Activity activi = (Activity) activity;
//				activi.finish();
//				degree.dismiss();
			}
		});
		
		degree.show();
		
	}
	
	
}
